package com.kgc.javaweb.dao.impl;


import cn.hutool.json.JSONUtil;

import com.mysql.jdbc.StringUtils;
import com.kgc.javaweb.dao.CarChargeDao;
import com.kgc.javaweb.dto.UserPageDTO;
import com.kgc.javaweb.entity.CarChargeInfo;
import com.kgc.javaweb.enums.CodeMsg;
import com.kgc.javaweb.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class CarChargeDaoImpl implements CarChargeDao {

    @Override
    public Long selectTotal(UserPageDTO userPageDTO) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select  count(*) from carpay where 1=1";
        String searchParams = userPageDTO.getSearchParams();
        System.out.println("searchParams = " + searchParams);
        ArrayList<Object> list = new ArrayList<>();
        if (searchParams != null){
            CarChargeInfo carChargeInfo = JSONUtil.toBean(searchParams, CarChargeInfo.class);
            Integer status = Integer.valueOf(carChargeInfo.getStatus());
            String param = (status == 0)?"未缴费":"已缴费";
            if (!StringUtils.isEmptyOrWhitespaceOnly(param)){
                sql += " and status = ?";
                list.add(param);
            }
        }
        try {
            Long total = queryRunner.query(sql, new ScalarHandler<Long>(),list.toArray());
            return total;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException("查询总条数失败");
        }
    }


    @Override
    public List<CarChargeInfo> selectPage(UserPageDTO userPageDTO) {
        Integer limit = userPageDTO.getLimit();
        Integer page = userPageDTO.getPage();
        Integer start = (page - 1) * limit;

        String searchParams = userPageDTO.getSearchParams();
        System.out.println("searchParams = " + searchParams);
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select  * from carpay where 1=1";
        ArrayList<Object> params = new ArrayList<>();

        if (searchParams != null) {
            CarChargeInfo carChargeInfo = JSONUtil.toBean(searchParams, CarChargeInfo.class);
            Integer status = Integer.valueOf(carChargeInfo.getStatus());
            System.out.println("status = " + status);
            String param = (status == 0)?"未缴费":"已缴费";
            System.out.println("param = " + param);
            if (!StringUtils.isEmptyOrWhitespaceOnly(param)) {
                sql += " and status = ?";
                params.add(param);
            }
        }
        sql += " limit ? , ?";
        params.add(start);
        params.add(limit);

        try {
            List<CarChargeInfo> carChargeInfos = queryRunner.query(sql, new BeanListHandler<CarChargeInfo>(CarChargeInfo.class), params.toArray());
            return  carChargeInfos;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new  RuntimeException("分页查询失败");
        }
    }

    @Override
    public void delete(String ids) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "delete from carpay where id = ?";

        String[] split = ids.split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(Integer.valueOf(s));
        }
        for (Integer id : list) {
            try {
                queryRunner.update(sql, id);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                throw new RuntimeException(CodeMsg.DELETE_FAIL.getMsg());
            }
        }
    }

    @Override
    public void update(CarChargeInfo carChargeInfo) {
        System.out.println(carChargeInfo.getStarttime());
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "update carpay set starttime=?,endtime=?,money=?,status=? where id =?";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startTime = simpleDateFormat.format(carChargeInfo.getStarttime());
            String endTime = simpleDateFormat.format(carChargeInfo.getEndtime());
            queryRunner.update(sql, startTime, endTime, carChargeInfo.getMoney(), carChargeInfo.getStatus(), carChargeInfo.getId());
            System.out.println("sql = " + sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException("修改失败!");
        }
    }

    @Override
    public void add(CarChargeInfo carChargeInfo) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into carpay(carnum,caruser,starttime,endtime,money,status) values(?,?,?,?,?,?)";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = simpleDateFormat.format(carChargeInfo.getStarttime());
        String endTime = simpleDateFormat.format(carChargeInfo.getEndtime());

        try {
            queryRunner.update(sql, carChargeInfo.getCarnum(), carChargeInfo.getCaruser(), startTime, endTime, carChargeInfo.getMoney(), carChargeInfo.getStatus());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public CarChargeInfo selectById(Integer id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select *  from carpay where id = ?";
        try {
            CarChargeInfo carChargeInfo = queryRunner.query(sql, new BeanHandler<CarChargeInfo>(CarChargeInfo.class), id);
            return carChargeInfo;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException("查询数据失败！");
        }
    }

    @Override
    public CarChargeInfo selectByCarName(Object carnum) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select * from carpay where carnum = ?";
        try {
            CarChargeInfo carChargeInfo = queryRunner.query(sql, new BeanHandler<CarChargeInfo>(CarChargeInfo.class), carnum);
            return carChargeInfo;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException("添加失败");
        }
    }

}
